package com.example.cardgame
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.cardgame.model.COST_TO_CHOOSE
import com.example.cardgame.model.CardMatchingGame
import com.example.cardgame.model.MATCH_BONUS
import com.example.cardgame.model.MISMATCH_PENALTY


const val FIRST_MSG="FIRST_MSG"
var game: CardMatchingGame= CardMatchingGame(32) //将game全局化，可以使支持横屏竖屏不同牌数


class MainActivity : AppCompatActivity() {
    //第二种方法支持横屏竖屏不同牌数
//    companion object{
//        var game: CardMatchingGame= CardMatchingGame(32)
//    }

    private lateinit var score: TextView
    private lateinit var adapter: CardAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val recyclerView:RecyclerView=findViewById(R.id.recyclerView)
        val reset: Button = findViewById(R.id.reset)
        val exit:Button=findViewById(R.id.exit)
        adapter=CardAdapter(game)
        score = findViewById(R.id.textView)
        recyclerView.adapter=adapter

        val configure=resources.configuration
        if(configure.orientation==Configuration.ORIENTATION_PORTRAIT){
            recyclerView.layoutManager=GridLayoutManager(this,4)
        }else{
            recyclerView.layoutManager=GridLayoutManager(this,8)
        }


        adapter.setOnClickedListener {
            game.chooseCardAtIndex(it)
            updateUI()
        }




        updateUI()

        reset.setOnClickListener {
            game.reset()
            updateUI()
        }
        exit.setOnClickListener {
            val intent = Intent()
            val scoreValue = game.score
            intent.putExtra(FIRST_MSG, scoreValue)
            setResult(2, intent)
            finish()
        }


    }
    // 显示游戏介绍
    private fun showGameInstructions() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("游戏介绍")
        builder.setMessage(
            "欢迎来到卡牌匹配游戏！\n\n" +
                    "游戏规则：\n" +
                    "1. 点击卡牌来翻转。\n" +
                    "2. 每次只能选择一张未匹配的卡牌。\n" +
                    "3. 当选择第二张卡牌时，将进行匹配判断。\n" +
                    "4. 如果两张卡牌匹配成功，它们将保持翻转状态并标记为匹配。\n" +
                    "5. 如果两张卡牌不匹配，它们将重新翻转。\n\n" +
                    "得分规则：\n" +
                    "1. 每次选择一张卡牌会消耗 $COST_TO_CHOOSE 分。\n" +
                    "2. 成功匹配两张卡牌时，根据匹配情况得分：\n" +
                    "   a) 如果两张卡牌的数字相同，获得 ${4 * MATCH_BONUS} 分。\n" +
                    "   b) 如果两张卡牌的花色相同，获得 ${1 * MATCH_BONUS} 分。\n" +
                    "3. 如果两张卡牌不匹配，失去 $MISMATCH_PENALTY 分。\n\n" +
                    "祝你好运！"
        )
        builder.setPositiveButton("开始游戏") { dialog: DialogInterface, _: Int ->
            dialog.dismiss()
        }
        builder.setCancelable(false)
        builder.show()
    }

    @SuppressLint("DefaultLocale")
    fun updateUI() {
        adapter.notifyDataSetChanged()
        score.text = String.format("Score: %d", game.score)
    }
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_game_rules -> {

                showGameInstructions()
                return true
            }
            R.id.action_background_music -> {


                return true
            }
            R.id.action_game_background -> {

                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }





}